package com.cskaoyan.service.impl;

import com.cskaoyan.bean.pojo.MarketOrderExample;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.pojo.MarketUserExample;
import com.cskaoyan.bean.vo.AuthIndexVO;
import com.cskaoyan.bean.bo.AuthLoginBO;
import com.cskaoyan.bean.vo.AuthLoginUserInfoVO;
import com.cskaoyan.bean.vo.AuthLoginVO;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.UserIndexService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author rebaogu
 * @description wx 前端实现类
 * @date 2022.04.08 17:26
 * @package com.cskaoyan.service.impl
 * @Classname UserIndexServiceImpl
 */
@Service
public class UserIndexServiceImpl implements UserIndexService {

    // 注册usermapper
    @Autowired
    MarketUserMapper marketUserMapper;

    // 注册订单mapper
    @Autowired
    MarketOrderMapper marketOrderMapper;


    @Override
    public AuthLoginVO authLogin(AuthLoginBO authLoginBO) {

        MarketUserExample marketUserExample = new MarketUserExample();

        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andUsernameEqualTo(authLoginBO.getUsername());

        List<MarketUser> user = marketUserMapper.selectByExample(marketUserExample);

        AuthLoginVO authLoginVO = new AuthLoginVO();
        authLoginVO.setUserInfo(new AuthLoginUserInfoVO(user.get(0).getUsername(),user.get(0).getAvatar()));

        return authLoginVO;
    }

    @Override
    public AuthIndexVO authIndex(MarketUser marketUser) {


        MarketOrderExample example2Unpaid = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example2Unpaid.createCriteria();
        // 101 --> 代付款  102 --> 用户取消 103 --> 系统取消
        criteria.andDeletedEqualTo(false);
        criteria.andOrderStatusEqualTo((short) 101);
        criteria.andUserIdEqualTo(marketUser.getId());

        MarketOrderExample example2Unship = new MarketOrderExample();
        MarketOrderExample.Criteria criteria2Unship = example2Unship.createCriteria();
        // 201 202 --> 代发货
        criteria2Unship.andDeletedEqualTo(false);
        criteria2Unship.andOrderStatusGreaterThanOrEqualTo((short) 201).andOrderStatusLessThanOrEqualTo((short) 202);
        criteria2Unship.andUserIdEqualTo(marketUser.getId());

        MarketOrderExample example2Unrecv = new MarketOrderExample();
        MarketOrderExample.Criteria criteria2Unrecv = example2Unrecv.createCriteria();
        // 301 --> 代收货
        criteria2Unrecv.andDeletedEqualTo(false);
        criteria2Unrecv.andOrderStatusEqualTo((short) 301);
        criteria2Unrecv.andUserIdEqualTo(marketUser.getId());

        MarketOrderExample example2Uncomment = new MarketOrderExample();
        MarketOrderExample.Criteria criteria2Uncomment = example2Uncomment.createCriteria();
        // 401 ,402 --> 代评价
        criteria2Uncomment.andDeletedEqualTo(false);
        criteria2Uncomment.andOrderStatusGreaterThanOrEqualTo((short) 401).andOrderStatusLessThanOrEqualTo((short) 402);
        criteria2Uncomment.andUserIdEqualTo(marketUser.getId());

        Integer count2Unrecv = Math.toIntExact(marketOrderMapper.countByExample(example2Unrecv));
        Integer count2Uncomment = Math.toIntExact(marketOrderMapper.countByExample(example2Uncomment));
        Integer count2Unpaid = Math.toIntExact(marketOrderMapper.countByExample(example2Unpaid));
        Integer count2Unship = Math.toIntExact(marketOrderMapper.countByExample(example2Unship));

        AuthIndexVO.OrderDTO orderDTO
                = new AuthIndexVO.OrderDTO(count2Unrecv, count2Uncomment, count2Unpaid, count2Unship);

        return new AuthIndexVO(orderDTO);
    }
}
